package com.student.utils;

import com.sun.deploy.net.DownloadException;

import java.sql.Connection;
import java.sql.DriverManager;

//负责创建数据库连接
public class DBUntil {
    public static void main(String[] args) {

    }
    private String account;
    private String password;
    private String db;

    public DBUntil(String account, String password, String db) {
        this.account = account;
        this.password = password;
        this.db = db;
        init();
    }
    public static Connection con=null;
    private void init(){
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("加载驱动成功");
        }catch(Exception e){
            System.out.println("加载驱动失败");
        }
        //连接数据库
        try{
            String url="jdbc:mysql://localhost:3306/"+db+"?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
            con= DriverManager.getConnection(url,account,password);
            Class.forName("com.mysql.cj.jdbc.Driver");
            System.out.println("连接成功");
        }catch(Exception e){
            System.out.println("连接失败");
            String temp =e.getMessage();
System.out.println(temp);
String[] arr1=temp.split("");
if (arr1[0].equals("Unknown")){
    System.out.println("请建立数据库"+arr1[2]);
}
if (arr1[0].equals("Access")){
                System.out.println("请检查密码是否正确");
            }

        }

    }
}
